package com.gwsdk.paysdk.http.request;
import android.text.TextUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.gwsdk.paysdk.bean.AntiAddictionModel;
import com.gwsdk.paysdk.http.RequestUtil;
import com.gwsdk.paysdk.utils.GWLog;

import org.json.JSONObject;

public class GetAgeResultRequest {

    private static final String TAG = "GetAgeResultRequest";

    HttpUtils http;

    public GetAgeResultRequest() {
        http = new HttpUtils();
    }

    public void post(String url, RequestParams params) {
        if (TextUtils.isEmpty(url) || null == params) {
            GWLog.e(TAG, "fun#post url is null add params is null");
            return;
        }
        GWLog.e(TAG, "fun#post url " + url);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = RequestUtil.getResponse(responseInfo);
                try {
                    JSONObject json = new JSONObject(response);
                    int status = json.optInt("code");
                    String msg = json.optString("msg");
                    if (status == 200) {  //查询国家平台实名认证状态：认证成功了
                        if (AntiAddictionModel.myTimeUtil != null){
                            AntiAddictionModel.myTimeUtil.Stop();
                        }
                    } else if (status == 2001){ //认证等待中
                        GWLog.e(TAG, "查询认证状态：审核中...");
                    } else {  //认证失败
                        GWLog.e(TAG, "查询认证状态：认证失败:" + msg);
                    }
                } catch (Exception e) {
                    GWLog.e(TAG,"网络异常:" + e.toString());
                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {
                GWLog.e(TAG, "fun#onFailure error = " + error.getExceptionCode());
                GWLog.e(TAG, "onFailure" + msg);
            }
        });
    }
}
